package com.itboye.system.filter;

import com.itboye.system.crypt.JsonCrypt;
import com.itboye.system.helper.ByConsoleHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.util.Map;

/**
 * 记录API请求的日志
 * Created by hebidu on 16/5/16.
 *
 */
@WebFilter(filterName = "RequestLogFilter")
public class RequestLogFilter implements Filter {

    private Logger logger = LogManager.getLogger(RequestLogFilter.class.toString());

    public void destroy() {

    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        logRequest(req);

        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

    /**
     *
     * 记录请求信息
     * @param req
     */
    private void logRequest(ServletRequest req){

        Map<String, String[]> params =  req.getParameterMap();

        String strParams = JsonCrypt.toJson(params);
        ByConsoleHelper.info(strParams,logger);
    }

}
